define([
    'jquery',
    'underscore',
    'backbone',
    'module/operator/models/OperatorModel',
    'module/operator/models/OperatorCollection',
    'module/helper/List',
    'text!module/operator/templates/main.html',
    'text!module/operator/templates/add.html',
    'text!module/operator/templates/edit.html',
    'text!module/operator/templates/drop.html',
    'text!module/operator/templates/list.html'
], function($, _, Backbone, OperatorModel, OperatorCollection, List, main, add, edit, drop, list) {

    var OperatorController = Backbone.View.extend({
        el: $("#page"),
        initialize: function() {
            this.render();
            this.list_view(1);
        },
        events: {
            "click #list_view": "list_view",
            "click #add_view": "add_view",
            "click #edit_view": "edit_view",
            "click #drop_view": "drop_view",
            "click #add_action": "add_action",
            "click #edit_action": "edit_action",
            "click #drop_action": "drop_action",
            "click #search": "search",
            "click #pag": "pag_action",
            "click #cancel_action": "cancel_action"
        },
        render: function() {
            this.$el.html(main);
            $("#title").html(this.options.title);

        },
        list_view: function(pag) {
            $.get('server/api/operator/find_all/' + this.options.type + '/' + pag, function(data) {
                var collection = new OperatorCollection(data.result);
                var listv = new List({collection: collection, count: data.count, pag: pag, list: list});
                listv.render();
            }, "json");
        },        
        add_view: function() {
            $("#main-list").html(add);
        },
        edit_view: function(event) {
            $("#main-list").html(edit);
            var id = event.target.getAttribute('index');
            var operator = new OperatorModel({id: id});
            operator.fetch({
                silent: true,
                success: function(data) {
                    var data = data.toJSON();
                    $("#id_model").val(data.id);
                    $("#name").val(data.name);
                    $("#code").val(data.code);
                    $("#address").val(data.address);
                    $("#fono1").val(data.fono1);
                            $("#fono2").val(data.fono2);
                            $("#contact").val(data.contact);
                            $("#email").val(data.email);
                            $("#pagweb").val(data.pagweb);
                            $("#description").val(data.description);
                },
                error: function(data) {
                    alert("Error en cargar datos a Modificar : " + data.id);
                }
            });
        },
        drop_view: function(event) {
            $("#main-list").html(drop);
            var id = event.target.getAttribute('index');
            var operator = new OperatorModel({id: id});
            operator.fetch({
                silent: true,
                success: function(data) {
                    data = data.toJSON();
                    $("#id").val(data.id);
                    $("#name").val(data.name);
                    $("#code").val(data.code);
                    $("#address").val(data.address);
                }
            });
        },
        add_action: function() {
            var self = this;
            var operator = new OperatorModel();
            operator.on('error', this.renderErrors, this);

            var operatorDetails = {
                name: $("#name").val(),
                code: $("#code").val(),
                address: $("#address").val(),
                //fono1, fono2, contact, email, pagweb, description
                type: this.options.type,
                fono1: $("#fono1").val(),
                fono2: $("#fono2").val(),
                contact: $("#contact").val(),
                email: $("#email").val(),
                pagweb: $("#pagweb").val(),
                description: $("#description").val()

            };
            operator.save(operatorDetails, {
                success: function(data) {
                    alert(self.options.title + " Guardado");
                    self.list_view(1);
                }
            });
        },
        edit_action: function() {
            var self = this;
            var operator = new OperatorModel();
            operator.on('error', this.renderErrors, this);
            var operatorDetails = {
                id: $("#id_model").val(),
                name: $("#name").val(),
                code: $("#code").val(),
                address: $("#address").val(),
                type: this.options.type,
                fono1: $("#fono1").val(),
                fono2: $("#fono2").val(),
                contact: $("#contact").val(),
                email: $("#email").val(),
                pagweb: $("#pagweb").val(),
                description: $("#description").val()
            };
            operator.save(operatorDetails, {
                success: function(data) {
                    alert(self.options.title + " Modificado");
                    self.list_view(1);
                },
                error: function(data) {
                    alert("Error en Modificar");
                }
            });
        },
        drop_action: function() {
            var self = this;
            var operator = new OperatorModel({
                id: $("#id").val()
            });
            operator.destroy({
                success: function(data) {
                    alert(self.options.title + " Eliminado");
                    self.list_view(1);
                }
            });
        },
        search: function() {
            var search = $('#txt_search').val();
            $.get('server/api/operator/search/' + search, function(data) {
                var collection = new OperatorCollection(data.result);
                var listv = new List({collection: collection, count: data.count, list: list});
                listv.render();
            }, "json");            
        },
        pag_action: function(event) {
            var pag = event.target.getAttribute('index');
            this.list_view(pag);
        },        
        cancel_action: function() {
            this.list_view(1);
        },
        renderErrors: function(model, errors) {
            this.$('.control-group').removeClass('error');
            this.$('.help-inline').remove();
            _.each(errors, function(messages, fieldName) {
                var filed = $('#' + fieldName);
                filed.parent().parent().addClass('error');
                filed.parent().append("<span class=\"help-inline\">" + messages + "</span>");

            });
        }
    });
    return OperatorController;
});
